<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/16 0016
 * Time: 下午 17:38
 */
namespace Home\Model;
use Think\Model;
class MeetingRoomModel extends Model{
    public function get_meeting_syqk_info($date){
        /************获取所有可用的会议室*******/
        $data = $this->field('id,name,use_time')->where('type=1')->select();
        /**********循环每一个会议室取得相应的数据***********/
        foreach($data as $k=>$v){
            $time = explode(';',$v['use_time']);
            //获取每个会议室的每个时间段的总时长
            $timelist = $this->get_duration($time);
            //dump($timelist);
            $data[$k]['use_time'] = $timelist[0]; //每个时间段的可用时间
            $data[$k]['duration'] = $duration = $timelist[1]; //每个时间段的时长

            //获取当天每个会议室的每个时间段已经使用的时长
            $use = $this->get_meeting_room_syqk($v['id'],$date);
            //dump($use);exit;
            //计算出当天每个会议室的每个时间段还剩余的时长
            $data[$k]['yi_sy'] = $use;
            $data[$k]['syqk'] = array(
                '上午' => (($duration['上午'] - $use['上午']) < 1800)?'已满':'未满',
                '下午' => (($duration['下午'] - $use['下午']) < 1800)?'已满':'未满',
                '晚上' => (($duration['晚上'] - $use['晚上']) < 1800)?'已满':'未满',

            );
        }
        //dump($data);exit;
        return $data;
    }

    /*
     * 获取单个会议室的每个时间段的可用时间总时长
     */
    public function get_duration($time){
        $aa = [];
        $bb = [];
        foreach($time as $k1=>$v1){
            $a = explode('/',$v1);
            $c = explode('--',$a[1]);

            $d = strtotime($c[1]) - strtotime($c[0]);
            $bb[$a[0]] = $d;
            $aa[$a[0]] = $a[1];
        }
        return array(
            $aa,$bb
        );
    }

    /*
     * 获取某一天的某个会议室的每个时间段的已经使用的时长
     */
    public function get_meeting_room_syqk($meeting_room_id,$date){
        $meeting_syqk = D('meeting_syqk');
        $where = "meeting_room_id={$meeting_room_id} and date='{$date}'";
        $syqk = $meeting_syqk->where($where)->select();
        //$data[$k]['syqk'] = $syqk;
        $sw = $xw = $ws = 0;

        if($syqk){
            foreach($syqk as $k2=>$v2){
                if($v2['date_modle'] == '上午') {
                    $e = explode('--', $v2['date_time']);
                    $sw += strtotime($e[1]) - strtotime($e[0]);
                }
                if($v2['date_modle'] == '下午') {
                    $e = explode('--', $v2['date_time']);
                    $xw += strtotime($e[1]) - strtotime($e[0]);
                }
                if($v2['date_modle'] == '晚上') {
                    $e = explode('--', $v2['date_time']);
                    $ws += strtotime($e[1]) - strtotime($e[0]);
                }
            }
        }
        $use = array(
            '上午' => $sw,
            '下午' => $xw,
            '晚上' => $ws
        );
        return $use;
    }

}